

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.ico">
  <link rel="icon" href="/img/favicon.ico">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="下面是我整理的常用 Git 命令清单。几个专用名词的译名如下。


Workspace：工作区
Index / Stage：暂存区
Repository：仓库区（或本地仓库）
Remote：远程仓库">
  <meta name="author" content="kinzun">
  <meta name="keywords" content>
  <meta name="description" content="下面是我整理的常用 Git 命令清单。几个专用名词的译名如下。   Workspace：工作区 Index / Stage：暂存区 Repository：仓库区（或本地仓库） Remote：远程仓库">
<meta name="keywords" content="Git">
<meta property="og:type" content="article">
<meta property="og:title" content="Git常用指令">
<meta property="og:url" content="https://kinzun.github.io/2017/06/03/Git_bash/index.html">
<meta property="og:site_name" content="kinzun">
<meta property="og:description" content="下面是我整理的常用 Git 命令清单。几个专用名词的译名如下。   Workspace：工作区 Index / Stage：暂存区 Repository：仓库区（或本地仓库） Remote：远程仓库">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://kinzun.github.io/images/GIT/gitwork.png">
<meta property="og:updated_time" content="2019-01-08T10:05:05.034Z">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="Git常用指令">
<meta name="twitter:description" content="下面是我整理的常用 Git 命令清单。几个专用名词的译名如下。   Workspace：工作区 Index / Stage：暂存区 Repository：仓库区（或本地仓库） Remote：远程仓库">
<meta name="twitter:image" content="https://kinzun.github.io/images/GIT/gitwork.png">
  
  <title>Git常用指令 - kinzun</title>

  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css">


  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css">
  <link rel="stylesheet" href="/lib/hint/hint.min.css">

  
    
    
      
      <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css">
    
  

  
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css">
  


<!-- 主题依赖的图标库，不要自行修改 -->
<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">

<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">

<link rel="stylesheet" href="/css/main.css">

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"kinzun.github.io","root":"/","version":"1.8.12","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname"}},"search_path":"/local-search.xml"};
  </script>
  <script src="/js/utils.js"></script>
  <script src="/js/color-schema.js"></script>
</head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>kinzun</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/img/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="Git常用指令">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2017-06-03 15:09" pubdate>
        2017年6月3日 下午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      3.7k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      12 分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Git常用指令</h1>
            
            <div class="markdown-body">
              <p>下面是我整理的常用 Git 命令清单。几个专用名词的译名如下。</p>
<p><img src="/images/GIT/gitwork.png" srcset="/img/loading.gif" lazyload alt></p>
<ul>
<li>Workspace：工作区</li>
<li>Index / Stage：暂存区</li>
<li>Repository：仓库区（或本地仓库）</li>
<li>Remote：远程仓库<a id="more"></a>
<h3 id="一、新建代码库"><a href="#一、新建代码库" class="headerlink" title="一、新建代码库"></a>一、新建代码库</h3><figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs undefined"><span class="hljs-meta">#</span><span class="bash"> 在当前目录新建一个Git代码库</span><br><span class="hljs-meta">$</span><span class="bash"> git init</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 新建一个目录，将其初始化为Git代码库</span><br><span class="hljs-meta">$</span><span class="bash"> git init [project-name]</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 下载一个项目和它的整个代码历史</span><br><span class="hljs-meta">$</span><span class="bash"> git <span class="hljs-built_in">clone</span> [url]</span><br></code></pre></div></td></tr></table></figure>
</li>
</ul>
<h3 id="二、配置"><a href="#二、配置" class="headerlink" title="二、配置"></a>二、配置</h3><p>Git的设置文件为.gitconfig，它可以在用户主目录下（全局配置），也可以在项目目录下（项目配置）。<br><figure class="highlight routeros"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs undefined"><span class="hljs-comment"># 显示当前的Git配置</span><br>$ git<span class="hljs-built_in"> config </span>--list<br><br> # 编辑Git配置文件<br>$ git<span class="hljs-built_in"> config </span>-e [--global]<br><br><span class="hljs-comment"># 设置提交代码时的用户信息</span><br>$ git<span class="hljs-built_in"> config </span>[--global] user.name <span class="hljs-string">"[name]"</span><br>$ git<span class="hljs-built_in"> config </span>[--global] user.email <span class="hljs-string">"[email address]"</span><br></code></pre></div></td></tr></table></figure></p>
<h3 id="三、增加-删除文件"><a href="#三、增加-删除文件" class="headerlink" title="三、增加/删除文件"></a>三、增加/删除文件</h3><figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs undefined"><span class="hljs-meta">#</span><span class="bash"> 添加指定文件到暂存区</span><br><span class="hljs-meta">$</span><span class="bash"> git add [file1] [file2] ...</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 添加指定目录到暂存区，包括子目录</span><br><span class="hljs-meta">$</span><span class="bash"> git add [dir]</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 添加当前目录的所有文件到暂存区</span><br><span class="hljs-meta">$</span><span class="bash"> git add .</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 删除工作区文件，并且将这次删除放入暂存区</span><br><span class="hljs-meta">$</span><span class="bash"> git rm [file1] [file2] ...</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 停止追踪指定文件，但该文件会保留在工作区</span><br><span class="hljs-meta">$</span><span class="bash"> git rm --cached [file]</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 改名文件，并且将这个改名放入暂存区</span><br><span class="hljs-meta">$</span><span class="bash"> git mv [file-original] [file-renamed]</span><br></code></pre></div></td></tr></table></figure>
<h3 id="四、代码提交"><a href="#四、代码提交" class="headerlink" title="四、代码提交"></a>四、代码提交</h3><figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs undefined"><span class="hljs-meta">#</span><span class="bash"> 提交暂存区到仓库区</span><br><span class="hljs-meta">$</span><span class="bash"> git commit -m [message]</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 提交暂存区的指定文件到仓库区</span><br><span class="hljs-meta">$</span><span class="bash"> git commit [file1] [file2] ... -m [message]</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 提交工作区自上次commit之后的变化，直接到仓库区</span><br><span class="hljs-meta">$</span><span class="bash"> git commit -a</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 提交时显示所有diff信息</span><br><span class="hljs-meta">$</span><span class="bash"> git commit -v</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 使用一次新的commit，替代上一次提交</span><br><span class="hljs-meta">#</span><span class="bash"> 如果代码没有任何新变化，则用来改写上一次commit的提交信息</span><br><span class="hljs-meta">$</span><span class="bash"> git commit --amend -m [message]</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 重做上一次commit，并包括指定文件的新变化</span><br><span class="hljs-meta">$</span><span class="bash"> git commit --amend [file1] [file2] ...</span><br></code></pre></div></td></tr></table></figure>
<h3 id="五、分支"><a href="#五、分支" class="headerlink" title="五、分支"></a>五、分支</h3><figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs undefined"><span class="hljs-meta">#</span><span class="bash"> 列出所有本地分支</span><br><span class="hljs-meta">$</span><span class="bash"> git branch</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 列出所有远程分支</span><br><span class="hljs-meta">$</span><span class="bash"> git branch -r</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 列出所有本地分支和远程分支</span><br><span class="hljs-meta">$</span><span class="bash"> git branch -a</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 新建一个分支，但依然停留在当前分支</span><br><span class="hljs-meta">$</span><span class="bash"> git branch [branch-name]</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 新建一个分支，并切换到该分支</span><br><span class="hljs-meta">$</span><span class="bash"> git checkout -b [branch]</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 新建一个分支，指向指定commit</span><br><span class="hljs-meta">$</span><span class="bash"> git branch [branch] [commit]</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 新建一个分支，与指定的远程分支建立追踪关系</span><br><span class="hljs-meta">$</span><span class="bash"> git branch --track [branch] [remote-branch]</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 切换到指定分支，并更新工作区</span><br><span class="hljs-meta">$</span><span class="bash"> git checkout [branch-name]</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 建立追踪关系，在现有分支与指定的远程分支之间</span><br><span class="hljs-meta">$</span><span class="bash"> git branch --<span class="hljs-built_in">set</span>-upstream [branch] [remote-branch]</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 合并指定分支到当前分支</span><br><span class="hljs-meta">$</span><span class="bash"> git merge [branch]</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 选择一个commit，合并进当前分支</span><br><span class="hljs-meta">$</span><span class="bash"> git cherry-pick [commit]</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 删除分支</span><br><span class="hljs-meta">$</span><span class="bash"> git branch -d [branch-name]</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 删除远程分支</span><br><span class="hljs-meta">$</span><span class="bash"> git push origin --delete [branch-name]</span><br><span class="hljs-meta">$</span><span class="bash"> git branch -dr [remote/branch]</span><br></code></pre></div></td></tr></table></figure>
<h3 id="六、标签"><a href="#六、标签" class="headerlink" title="六、标签"></a>六、标签</h3><figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs undefined"><span class="hljs-meta">#</span><span class="bash"> 列出所有tag</span><br><span class="hljs-meta">$</span><span class="bash"> git tag</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 新建一个tag在当前commit</span><br><span class="hljs-meta">$</span><span class="bash"> git tag [tag]</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 新建一个tag在指定commit</span><br><span class="hljs-meta">$</span><span class="bash"> git tag [tag] [commit]</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 查看tag信息</span><br><span class="hljs-meta">$</span><span class="bash"> git show [tag]</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 提交指定tag</span><br><span class="hljs-meta">$</span><span class="bash"> git push [remote] [tag]</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 提交所有tag</span><br><span class="hljs-meta">$</span><span class="bash"> git push [remote] --tags</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 新建一个分支，指向某个tag</span><br><span class="hljs-meta">$</span><span class="bash"> git checkout -b [branch] [tag]</span><br></code></pre></div></td></tr></table></figure>
<h3 id="七、查看信息"><a href="#七、查看信息" class="headerlink" title="七、查看信息"></a>七、查看信息</h3><figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs undefined"><span class="hljs-meta">#</span><span class="bash"> 显示有变更的文件</span><br><span class="hljs-meta">$</span><span class="bash"> git status</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 显示当前分支的版本历史</span><br><span class="hljs-meta">$</span><span class="bash"> git <span class="hljs-built_in">log</span></span><br><span class="hljs-meta"><br>#</span><span class="bash"> 显示commit历史，以及每次commit发生变更的文件</span><br><span class="hljs-meta">$</span><span class="bash"> git <span class="hljs-built_in">log</span> --<span class="hljs-built_in">stat</span></span><br><span class="hljs-meta"><br>#</span><span class="bash"> 显示某个文件的版本历史，包括文件改名</span><br><span class="hljs-meta">$</span><span class="bash"> git <span class="hljs-built_in">log</span> --follow [file]</span><br><span class="hljs-meta">$</span><span class="bash"> git whatchanged [file]</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 显示指定文件相关的每一次diff</span><br><span class="hljs-meta">$</span><span class="bash"> git <span class="hljs-built_in">log</span> -p [file]</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 显示指定文件是什么人在什么时间修改过</span><br><span class="hljs-meta">$</span><span class="bash"> git blame [file]</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 显示暂存区和工作区的差异</span><br><span class="hljs-meta">$</span><span class="bash"> git diff</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 显示暂存区和上一个commit的差异</span><br><span class="hljs-meta">$</span><span class="bash"> git diff --cached [file]</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 显示工作区与当前分支最新commit之间的差异</span><br><span class="hljs-meta">$</span><span class="bash"> git diff HEAD</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 显示两次提交之间的差异</span><br><span class="hljs-meta">$</span><span class="bash"> git diff [first-branch]...[second-branch]</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 显示某次提交的元数据和内容变化</span><br><span class="hljs-meta">$</span><span class="bash"> git show [commit]</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 显示某次提交发生变化的文件</span><br><span class="hljs-meta">$</span><span class="bash"> git show --name-only [commit]</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 显示某次提交时，某个文件的内容</span><br><span class="hljs-meta">$</span><span class="bash"> git show [commit]:[filename]</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 显示当前分支的最近几次提交</span><br><span class="hljs-meta">$</span><span class="bash"> git reflog</span><br></code></pre></div></td></tr></table></figure>
<h3 id="八、远程同步"><a href="#八、远程同步" class="headerlink" title="八、远程同步"></a>八、远程同步</h3><figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs undefined"><span class="hljs-meta">#</span><span class="bash"> 下载远程仓库的所有变动</span><br><span class="hljs-meta">$</span><span class="bash"> git fetch [remote]</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 显示所有远程仓库</span><br><span class="hljs-meta">$</span><span class="bash"> git remote -v</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 显示某个远程仓库的信息</span><br><span class="hljs-meta">$</span><span class="bash"> git remote show [remote]</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 增加一个新的远程仓库，并命名</span><br><span class="hljs-meta">$</span><span class="bash"> git remote add [shortname] [url]</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 取回远程仓库的变化，并与本地分支合并</span><br><span class="hljs-meta">$</span><span class="bash"> git pull [remote] [branch]</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 上传本地指定分支到远程仓库</span><br><span class="hljs-meta">$</span><span class="bash"> git push [remote] [branch]</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 强行推送当前分支到远程仓库，即使有冲突</span><br><span class="hljs-meta">$</span><span class="bash"> git push [remote] --force</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 推送所有分支到远程仓库</span><br><span class="hljs-meta">$</span><span class="bash"> git push [remote] --all</span><br></code></pre></div></td></tr></table></figure>
<h3 id="九、撤销"><a href="#九、撤销" class="headerlink" title="九、撤销"></a>九、撤销</h3><figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs undefined"><span class="hljs-meta">#</span><span class="bash"> 恢复暂存区的指定文件到工作区</span><br><span class="hljs-meta">$</span><span class="bash"> git checkout [file]</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 恢复某个commit的指定文件到工作区</span><br><span class="hljs-meta">$</span><span class="bash"> git checkout [commit] [file]</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 恢复上一个commit的所有文件到工作区</span><br><span class="hljs-meta">$</span><span class="bash"> git checkout .</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 重置暂存区的指定文件，与上一次commit保持一致，但工作区不变</span><br><span class="hljs-meta">$</span><span class="bash"> git reset [file]</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 重置暂存区与工作区，与上一次commit保持一致</span><br><span class="hljs-meta">$</span><span class="bash"> git reset --hard</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 重置当前分支的指针为指定commit，同时重置暂存区，但工作区不变</span><br><span class="hljs-meta">$</span><span class="bash"> git reset [commit]</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 重置当前分支的HEAD为指定commit，同时重置暂存区和工作区，与指定commit一致</span><br><span class="hljs-meta">$</span><span class="bash"> git reset --hard [commit]</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 重置当前HEAD为指定commit，但保持暂存区和工作区不变</span><br><span class="hljs-meta">$</span><span class="bash"> git reset --keep [commit]</span><br><span class="hljs-meta"><br>#</span><span class="bash"> 新建一个commit，用来撤销指定commit</span><br><span class="hljs-meta">#</span><span class="bash"> 后者的所有变化都将被前者抵消，并且应用到当前分支</span><br><span class="hljs-meta">$</span><span class="bash"> git revert [commit]</span><br></code></pre></div></td></tr></table></figure>
<h3 id="十、其他"><a href="#十、其他" class="headerlink" title="十、其他"></a>十、其他</h3><figure class="highlight shell"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs undefined"><span class="hljs-meta">#</span><span class="bash"> 生成一个可供发布的压缩包</span><br><span class="hljs-meta">$</span><span class="bash"> git archive</span><br></code></pre></div></td></tr></table></figure>
<ul>
<li><p>忽略</p>
<figure class="highlight asciidoc"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs undefined">在工程根目录下建立.gitignore文件，将要排除的文件或目录 写到.gitignore这个文件中，其中有两种写入方法：<br><span class="hljs-section">一：<br>==================</span><br>单独项目<br>1. echo "<span class="hljs-strong">*.pyc" &gt; .gitignore # 例 排除以 .pyc 结尾的文件名<br>2. vim .gitignore <br>   *</span>~<br><span class="hljs-code">  .DS_Store</span><br><span class="hljs-code">  .idea</span><br><span class="hljs-code">  .venv</span><br><span class="hljs-code">  </span><br><br>===================<br><br>二：<br>全局设置排除文件：<br># git config --global core.excludesfile ~/.gitignore<br># vim .gitconfig<br>会有如下字段配置路径<br>[core]<br>		excludesfile = /Users/toorl/.gitignore_global<br>设置完成，重复方法一，设置排除路径。<br><br>===================<br><br>三：<br>单独工程设置排除文件。找到路径下 .git/info/exclude 。vim 编辑即可。<br></code></pre></div></td></tr></table></figure>
</li>
</ul>
<p>（完）</p>
<p><a href="https://github.com/kinzun/kinzun.github.io/blob/master/images/GIT/GIT%E5%AD%A6%E4%B9%A0%E8%84%91%E5%9B%BE.jpg" target="_blank" rel="noopener">Git思维导图</a></p>
<p>via：<a href="http://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html" target="_blank" rel="noopener">ruanyifeng.com</a></p>
<p><a href="http://rogerdudler.github.io/git-guide/index.zh.html" target="_blank" rel="noopener">git - 简明指南</a></p>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/Git/">Git</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2017/06/05/Git_advancedguide/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Git 进阶指南</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2017/05/27/ansibleo/">
                        <span class="hidden-mobile">ansible总结</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
              <!-- Comments -->
              <article class="comments" id="comments" lazyload>
                
                  
                
                

              </article>
            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  

  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/js/local-search.js" ></script>



  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
      typing(title)
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
